﻿using Come.CollectiveOAuth.Cache;
using Come.CollectiveOAuth.Config;
using Come.CollectiveOAuth.Enums;
using Come.CollectiveOAuth.Request;
using Come.CollectiveOAuth.Utils;
using PMS.Core.Configuration.CommonEnumConfigs;
using PMS.Core.Utils;

namespace PMS.Web.Framework.CollectiveAuthFactory
{
    /// <summary>
    /// 基于ColletiveAuth(https://gitee.com/rthinking/CollectiveOAuth)
    /// 实现第三方Oath登录授权
    /// </summary>
    public class CollectiveAuthFactory
    {
        #region Fields
        private readonly ClientConfig _clientConfig;
        private readonly DefaultAuthSourceEnum _authSourceEnum;
        private readonly IAuthStateCache _authStateCache;
        #endregion

        #region Ctor
        public CollectiveAuthFactory(ClientConfig cfg, string authSource)
        {
            _clientConfig = cfg;
            _authSourceEnum = GlobalAuthUtil.enumFromString<DefaultAuthSourceEnum>(authSource);
            _authStateCache = new DefaultAuthStateCache();
        }
        public CollectiveAuthFactory(ClientConfig cfg, DefaultAuthSourceEnum authSourceEnum)
        {
            _clientConfig = cfg;
            _authSourceEnum = authSourceEnum;
            _authStateCache = new DefaultAuthStateCache();
        }
        #endregion

        #region Methods
        /**
        * 返回AuthRequest对象
        *
        * @return {@link AuthRequest}
        */
        public IAuthRequest getRequest()
        {
            // 获取 CollectiveOAuth 中已存在的
            IAuthRequest authRequest = getDefaultRequest(_authSourceEnum);
            return authRequest;
        }


        /// <summary>
        /// 获取默认的 Request
        /// </summary>
        /// <param name="authSource"></param>
        /// <returns>{@link AuthRequest}</returns>
        private IAuthRequest getDefaultRequest(DefaultAuthSourceEnum authSource)
        {
            if (_clientConfig == null)
                throw new PMSException("_clientConfig cannot be Null", SystemLogLevel.Error);
            return authSource switch
            {
                DefaultAuthSourceEnum.WECHAT_MP => new WeChatMpAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.WECHAT_OPEN => new WeChatOpenAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.WECHAT_ENTERPRISE => new WeChatEnterpriseAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.WECHAT_ENTERPRISE_SCAN => new WeChatEnterpriseScanAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.ALIPAY_MP => new AlipayMpAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.GITEE => new GiteeAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.GITHUB => new GithubAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.BAIDU => new BaiduAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.XIAOMI => new XiaoMiAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.DINGTALK_SCAN => new DingTalkScanAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.OSCHINA => new OschinaAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.CODING => new CodingAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.LINKEDIN => new LinkedInAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.WEIBO => new WeiboAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.QQ => new QQAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.DOUYIN => new DouyinAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.GOOGLE => new GoogleAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.FACEBOOK => new FackbookAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.MICROSOFT => new MicrosoftAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.TOUTIAO => new ToutiaoAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.TEAMBITION => new TeambitionAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.RENREN => new RenrenAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.PINTEREST => new PinterestAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.STACK_OVERFLOW => new StackOverflowAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.HUAWEI => new HuaweiAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.KUJIALE => new KujialeAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.GITLAB => new GitlabAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.MEITUAN => new MeituanAuthRequest(_clientConfig, _authStateCache),
                DefaultAuthSourceEnum.ELEME => new ElemeAuthRequest(_clientConfig, _authStateCache),
                _ => null
            };
        }
        #endregion
    }

}
